﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL.Common
{
    public class LogHelper
    {
        private static LogHelper instance;
        public static LogHelper Instance()
        {
            log4net.Config.XmlConfigurator.Configure();
            instance = new LogHelper();
            return instance;
        }

        private static readonly log4net.ILog Loginfo = log4net.LogManager.GetLogger("loginfo");   //选择<logger name="loginfo">的配置 

        private static readonly log4net.ILog Logerror = log4net.LogManager.GetLogger("logerror");   //选择<logger name="logerror">的配置 

        private static readonly log4net.ILog Logsqlserver = log4net.LogManager.GetLogger("logsqlserver");   //选择<logger name="logsqlserver">的配置 

        /// <summary>
        /// 写系统信息日志
        /// </summary>
        /// <param name="info"></param>
        public void WriteLogSql(string info)
        {
            LoginUser currentLoginUser = AppUserSession.Instance().GetCurrentUser();
            if (Loginfo.IsInfoEnabled)
            {
                try
                {
                    log4net.LogicalThreadContext.Properties["UserAccount"] = currentLoginUser.Name;
                    log4net.LogicalThreadContext.Properties["ClientIP"] = currentLoginUser.LoginIp;
                    log4net.LogicalThreadContext.Properties["UserId"] = currentLoginUser.UserId;
                }
                catch (Exception)
                {
                    log4net.LogicalThreadContext.Properties["UserAccount"] = "系统后台";
                    log4net.LogicalThreadContext.Properties["ClientIP"] = "系统后台";
                    log4net.LogicalThreadContext.Properties["UserId"] = "系统后台";
                    //throw;
                }

                Logsqlserver.Info(info);
            }
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="error"></param>
        /// <param name="se"></param>
        public void WriteLogSql(string error, Exception se)
        {
            LoginUser currentLoginUser = AppUserSession.Instance().GetCurrentUser();
            if (Loginfo.IsInfoEnabled)
            {
                try
                {
                    log4net.LogicalThreadContext.Properties["UserAccount"] = currentLoginUser.Name;
                    log4net.LogicalThreadContext.Properties["ClientIP"] = currentLoginUser.LoginIp;
                    log4net.LogicalThreadContext.Properties["UserId"] = currentLoginUser.UserId;
                }
                catch (Exception)
                {
                    log4net.LogicalThreadContext.Properties["UserAccount"] = "系统后台";
                    log4net.LogicalThreadContext.Properties["ClientIP"] = "系统后台";
                    log4net.LogicalThreadContext.Properties["UserId"] = "系统后台";
                    //throw;
                }
                Logsqlserver.Error(error, se);
            }
        }

        /// <summary>
        /// 写系统信息日志
        /// </summary>
        /// <param name="info"></param>
        public void WriteLog(string info)
        {
            if (Loginfo.IsInfoEnabled)
            {
                Loginfo.Info(info);
            }
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="error"></param>
        /// <param name="se"></param>
        public void WriteLog(string error, Exception se)
        {
            if (Logerror.IsErrorEnabled)
            {
                Logerror.Error(error, se);
            }
        }
    }
}
